package cc.rengu.igas.bomp.common.dao.impl;

import cc.rengu.igas.bomp.common.dao.BompMcInfoMapper;
import cc.rengu.igas.bomp.common.entity.BompMcInfo;
import cc.rengu.oltp.service.common.constant.AppConfigConstant;
import cc.rengu.oltp.service.common.constant.AppParamConstant;
import cc.rengu.oltp.utility.util.DbsUtil;
import cc.rengu.oltp.utility.util.XmlConfigUtil;
import cc.rengu.oltp.utility.util.XmlTreeUtil;
import cc.rengu.utility.cache.UnifiedCache;
import cc.rengu.utility.log.RgLog;
import cc.rengu.utility.log.RgLogger;

public class BompMcInfoMapperImpl implements BompMcInfoMapper {

    private RgLogger rglog = RgLog.getLogger(this.getClass().getName());
    String dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(AppParamConstant.DATABASE_CONF_NODE, AppConfigConstant.MOMP_POOL_NAME);

    @Override
    public BompMcInfo selectBompMcInfoByPrimaryKey(String instId, String mcNo) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MOMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "bompSelectBompMcMoInfoByPrimaryKey");
        BompMcInfo bompMcInfo = new BompMcInfo();
        bompMcInfo.setInstId(instId);
        bompMcInfo.setMcNo(mcNo);
        int iReturnCode = dbsUtil.dbsExecuteSql(sqlStr, bompMcInfo, bompMcInfo);
        if (0 != iReturnCode) {
            //查询减免类无活动,查卡券类
            sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "bompSelectBompMcCpInfoByPrimaryKey");
            iReturnCode = dbsUtil.dbsExecuteSql(sqlStr, bompMcInfo, bompMcInfo);
            if (0 != iReturnCode) {
                //查询卡券类无活动,无此编号活动
                rglog.error("获取营销活动信息失败,instId:<{}>,mcNo:<{}>", instId, mcNo);
                return null;
            }
        }
        return bompMcInfo;
    }

}
